package me.shurufa.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.finalteam.okhttpfinal.HttpRequest;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.db.PersistenceUtils;
import me.shurufa.model.LoginResponse;
import me.shurufa.net.API;
import me.shurufa.net.RequestParams;
import me.shurufa.net.okhttp.callback.HttpCallback;
import me.shurufa.response.LoginResp;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;

/**
 * 第三方授权登录后跳到这个界面
 */
public class ThirdPartyLoginFragment extends AuthenticatorFragment implements View.OnClickListener {

    @Bind(R.id.et_nickname)
    EditText et_nickname;

    @Bind(R.id.btn_regiest)
    Button btn_regiest;

    private String loginType, openid, accessToken, unionid;

    public static ThirdPartyLoginFragment newInstance(String loginType, String openid, String accessToken, String unionid) {
        ThirdPartyLoginFragment fragment = new ThirdPartyLoginFragment();
        Bundle args = new Bundle();
        args.putString("loginType", loginType);
        args.putString("openid", openid);
        args.putString("accessToken", accessToken);
        args.putString("unionid", unionid);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_third_party_login, container, false);
        ButterKnife.bind(this, view);
        parseArguments();
        return view;
    }

    private void parseArguments() {
        if (null != getArguments()) {
            loginType = getArguments().getString("loginType");
            openid = getArguments().getString("openid");
            accessToken = getArguments().getString("accessToken");
            unionid = getArguments().getString("unionid");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        btn_regiest.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_regiest:
                if (checkNickname()) {
                    regiest(et_nickname.getText().toString().trim());
                }
                break;
        }
    }

    private boolean checkNickname() {
        String nickname = et_nickname.getText().toString().trim();
        if (TextUtils.isEmpty(nickname)) {
            et_nickname.requestFocus();
            Utils.showToast("请输入昵称");
            return false;
        }

        if (nickname.length() > Constants.MAX_NAME_LENGTH) {
            et_nickname.requestFocus();
            Utils.showToast("昵称过长");
            return false;
        }

        if (nickname.length() < 2) {
            et_nickname.requestFocus();
            Utils.showToast("昵称太短");
            return false;
        }

        return true;
    }

    private void regiest(String username) {
        RequestParams params = new RequestParams();
        params.addFormDataPart("username", username);
        params.addFormDataPart("auth_type", loginType);
        params.addFormDataPart("openid", openid);
        params.addFormDataPart("token", accessToken);
        if (Constants.LOGIN_TYPE_WEIXIN.equalsIgnoreCase(loginType)) {
            params.addFormDataPart("unionid", unionid);
        }

        HttpRequest.post(API.AUTH_REG, params, new HttpCallback<LoginResp>() {
            @Override
            public void onRespSuccess(LoginResp result) {
                try {
                    PersistenceUtils.setUser(result.data);
                    EventBus.getDefault().post(new Intent(Constants.ACTION_LOGIN_SUCCESS));
                    getActivity().finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onRespFailure(LoginResp loginResp) {

            }

        });

    }

    @Override
    protected void saveUserInfo(LoginResponse result) {
        super.saveUserInfo(result);
    }

    @Override
    public void onEventMainThread(Intent intent) {
        super.onEventMainThread(intent);
    }
}
